package com.cms.manage.util;

import com.cms.manage.model.*;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.swing.plaf.synth.Region;


public class ListUtil {
    public static Long containCount(List<Long> list, Long number) {
        if (null == list || null == number) {
            return 0L;
        }
        Long count = 0L;
        for (Long aLong : list) {
            if (aLong.equals(number)) {
                count++;
            }
        }
        return count;
    }

    public static String toString(List<Long> list) {
        if (null == list) {
            return null;
        }
        StringBuffer result = new StringBuffer("");
        for (Long aLong : list) {
            if (result.length() > 0) {
                result.append(",");
            }
            result.append(aLong);
        }
        return result.toString();
    }

    public static String toStringInt(List<Integer> list) {
        if (null == list) {
            return null;
        }
        StringBuffer result = new StringBuffer("");
        for (Integer aInt : list) {
            if (result.length() > 0) {
                result.append(",");
            }
            result.append(aInt);
        }
        return result.toString();
    }

 
    public static List<Long> toListLong(String longs) {
        List<Long> result = new ArrayList<>();

        if (StringUtils.isBlank(longs)) {
            return result;
        }

        String[] split = longs.split(",");
        for (String aLong : split) {
            result.add(Long.valueOf(aLong));
        }
        return result;
    }

    public static List<Long> removeItem(List<Long> list, String longs) {
        if (null == list) {
            return null;
        }
        if (StringUtils.isBlank(longs)) {
            return list;
        }
        String[] longsArr = longs.split(",");
        for (String aLong : longsArr) {
            list.remove(Long.valueOf(aLong));
        }
        return list;
    }

    public static void main(String[] args) {
        List<Long> list = new ArrayList<>();
        list.add(1L);
        list.add(2L);
        list.add(3L);
        removeItem(list, "1,2");
        System.out.println(list);
    }

    public static List<Long> distinctItem(List<Long> examIds) {
        List<Long> r = new ArrayList<>();
        for (Long examId : examIds) {
            if (!r.contains(examId)) {
                r.add(examId);
            }
        }
        return r;
    }

    public static String toStringByKey(List<Map<String, Object>> list, String key) {
        if (list == null || list.isEmpty()) {
            return "";
        }
        StringBuilder str = new StringBuilder();
        for (Map<String, Object> map : list) {
            if (str.length() > 0) {
                str.append(",");
            }
            str.append(map.get(key));
        }
        return str.toString();
    }

    /**
     * 将中英文逗号分隔的字符串转为列表
     *
     * @param str
     * @return
     */
    public static List toList(String str) {
        if (StringUtils.isBlank(str)) {
            return null;
        }

        str = StringUtils.replace(str, "，", ",");

        String[] array = str.split(",");
        List list = new ArrayList();
        for (String s : array) {
            list.add(s);
        }
        return list;
    }
}
